package com.ruoyi.system.service.tumor.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.mapper.tumor.BussinessDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.tumor.ReportRecordMapper;
import com.ruoyi.system.domain.tumor.ReportRecord;
import com.ruoyi.system.service.tumor.IReportRecordService;

/**
 * 报告记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-09
 */
@Service
public class ReportRecordServiceImpl implements IReportRecordService
{
    @Autowired
    private ReportRecordMapper reportRecordMapper;

    @Autowired
    private BussinessDataMapper bussinessDataMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询报告记录
     *
     * @param id 报告记录主键
     * @return 报告记录
     */
    @Override
    public ReportRecord selectReportRecordById(Long id)
    {
        return reportRecordMapper.selectReportRecordById(id);
    }

    /**
     * 查询报告记录列表
     *
     * @param reportRecord 报告记录
     * @return 报告记录
     */
    @Override
    public List<ReportRecord> selectReportRecordList(ReportRecord reportRecord)
    {
        reportRecord.setSubscribeStatus("1");
        return reportRecordMapper.selectReportRecordList(reportRecord);
    }

    /**
     * 新增报告记录
     *
     * @param reportRecord 报告记录
     * @return 结果
     */
    @Override
    public int insertReportRecord(ReportRecord reportRecord)
    {
        reportRecord.setCreateTime(DateUtils.getNowDate());
        return reportRecordMapper.insertReportRecord(reportRecord);
    }

    /**
     * 修改报告记录
     *
     * @param reportRecord 报告记录
     * @return 结果
     */
    @Override
    public int updateReportRecord(ReportRecord reportRecord)
    {
        reportRecord.setUpdateTime(DateUtils.getNowDate());
        return reportRecordMapper.updateReportRecord(reportRecord);
    }

    /**
     * 批量删除报告记录
     *
     * @param ids 需要删除的报告记录主键
     * @return 结果
     */
    @Override
    public int deleteReportRecordByIds(Long[] ids)
    {
        return reportRecordMapper.deleteReportRecordByIds(ids);
    }

    /**
     * 删除报告记录信息
     *
     * @param id 报告记录主键
     * @return 结果
     */
    @Override
    public int deleteReportRecordById(Long id)
    {
        return reportRecordMapper.deleteReportRecordById(id);
    }

    @Override
    public List<Map> selectLimsReportListByPhone(Map map) {
        List<Map> list = new ArrayList<>();
        String phone = map.get("phone") == null?"":map.get("phone").toString();
        //0是检测中，1是已完成
        String status = map.get("status") == null?"":map.get("status").toString();
        if(StringUtils.isNotEmpty(phone)){
            if(StringUtils.isNotEmpty(status) && status.equals("0")){
                list = bussinessDataMapper.selectLimsReportingListByPhone(phone);
            }else if(StringUtils.isNotEmpty(status) && status.equals("1")){
                list = bussinessDataMapper.selectLimsReportedListByPhone(phone);
            }
        }

        //遍历list，根据订单编号查询生殖的数据库，找到对应的id和status
        if(CollectionUtil.isNotEmpty(list)){
            list.stream().forEach(map1 -> {
               String orderNo =  map1.get("orderNo") == null?"":map1.get("orderNo").toString();
               if(StringUtils.isNotEmpty(orderNo)){ //根据orderNo查询生殖数据库的id和status
                   ReportRecord reportRecord = reportRecordMapper.getReportRecordByOrderNo(orderNo);
                   if(reportRecord != null){ // 不为空
                       map1.put("id",reportRecord.getId());
                       map1.put("subscribeStatus",reportRecord.getSubscribeStatus());//预约状态，是否预约
                       if(StringUtils.isNotEmpty(reportRecord.getType())){
                           String name = sysDictDataMapper.selectDictLabel("sys_appointment_period",reportRecord.getType());
                           if(StringUtils.isNotEmpty(name)){
                               map1.put("time",name);
                           }
                       }
                       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                       if(reportRecord.getSubscribeDate()!=null){
                           map1.put("subscribeDate",reportRecord.getSubscribeDate()==null?"":simpleDateFormat.format(reportRecord.getSubscribeDate()));
                       }
                       map1.put("isInterpret",reportRecord.getIsInterpret());//是否解读
                   }
               }
            });
        }
        return list;
    }

    @Override
    public Map selectLimsReportByOrderNo(String orderNo) {
        return bussinessDataMapper.selectLimsReportByOrderNo(orderNo);
    }

    @Override
    public int saveReportRecord(ReportRecord reportRecord) {
        //根据orderNo查询生殖数据库判断是否存在数据，如果存在，则修改，不存在则新增
        String orderNo = reportRecord.getOrderNo();
        int i = 0;
        if(StringUtils.isNotEmpty(orderNo)){
            ReportRecord record = reportRecordMapper.getReportRecordByOrderNo(orderNo);
            if(record != null){
                Long id = record.getId();
                if(id != null){
                    reportRecord.setId(id);
                    reportRecord.setSubscribeStatus("1");
                    reportRecord.setIsInterpret("0");
                    reportRecord.setUpdateTime(new Date());
                    i = reportRecordMapper.updateReportRecord(reportRecord);
                }
            }else {
                reportRecord.setCreateTime(new Date());
                reportRecord.setSubscribeStatus("1");
                reportRecord.setIsInterpret("0");
                i = reportRecordMapper.insertReportRecord(reportRecord);
            }
        }
        return i;
    }

    @Override
    public int cancelReservation(Integer id) {
       return reportRecordMapper.cancelReservation(id);
    }

    @Override
    public int changeInterpretReport(Long id, String isInterpret) {
        return reportRecordMapper.changeInterpretReport(id,isInterpret);
    }
}
